package n叉树的遍历;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

/**
 * @author: zhengtao
 * @description: N叉树的后序遍历
 * @version: 1.0
 * @date: 2022/10/29  16:18
 */
public class Demo590 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    public List<Integer> postorder(Node root) {
        List<Integer> res=new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<Node> stack=new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            if(pop!=null){
                res.add(pop.val);
                List<Node> children = pop.children;
                for (int i = 0; i < children.size(); i++) {
                    stack.push(children.get(i));
                }
            }

        }
         Collections.reverse(res);
        return res;
    }

}
